<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" clearable placeholder="名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" size="medium" v-on:click="handleCurrentChange(1)">查询</el-button>
				</el-form-item>
				<el-form-item class="fr">
					<el-button type="primary" size="medium" plain @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="users" stripe highlight-current-row v-loading="listLoading" border @selection-change="selsChange" style="width: 100%;">
			<el-table-column prop="id" label="ID" min-width="30" align="center"></el-table-column>
			<el-table-column prop="name" label="名称" min-width="80" align="center"></el-table-column>
			<el-table-column label="操作" min-width="60" align="center">
				<template slot-scope="scope">
					<el-button type="danger" plain size="mini" @click="handleDels(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<!--<el-col :span="24" class="toolbar">
			
			<el-pagination background layout="prev, pager, next, jumper" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>-->
		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false" width="500px">
			<el-form :model="addForm" label-width="80px" ref="addForm">
				<el-form-item label="省" prop="province">
		          <el-select v-model="addForm.province" clearable placeholder="请选择省" filterable >
		            <el-option v-for="(item,index) in addrList" :key="item.value" :label="item.label" :value="item.value" @click.native="handleProvince(index)"></el-option>
		          </el-select>
		        </el-form-item>
		        <el-form-item label="市" prop="city">
		          <el-select v-model="addForm.city" clearable placeholder="请选择市" filterable >
		            <el-option v-for="(item,index) in cityList" :key="item.value" :label="item.label" :value="item.value" @click.native="handleCity(index)"></el-option>
		          </el-select>
		        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" @click.native="addFormVisible = false">取消</el-button>
				<el-button size="mini" type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false" width="500px">
			<el-form :model="editForm" label-width="80px" ref="editForm">
				<el-form-item label="名称" prop="title">
					<el-input  v-model="editForm.title" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="内容" prop="content">
					<el-input  v-model="editForm.content" type="textarea" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" @click.native="editFormVisible = false">取消</el-button>
				<el-button size="mini" type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	import { client } from '../../common/js/alioss'
	export default {
		data() {
			return {
				filters:{
					name:'',
				},
				sels:[],
				users: [],
				total: 0,
				page: 1,
				pageSize: 10,
				listLoading: false,
				addFormVisible: false,
				addLoading: false,
				addForm: {
					province: '',
					city:'',
					name:'',
					regionId:'',
				},
				editFormVisible: false,
				editLoading: false,
				editForm: {
					id: '',
					province: '',
					city:'',
					name:'',
					regionId:'',
				},
				addrList:[],
				cityList:'',
				provinceIndex:'',
				cityIndex:''
			}
		},
		methods: {
			
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers();
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//获取列表
			getUsers:function (){
				this.listLoading = true;
				let para ={
					name:this.filters.name
				}
				var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
				});  
				instance.post('admin/logistics/getNoDeliveryRegion', this.qs.stringify(para)).then(data => {
					let  res = data.data;
					this.users = res;
					this.listLoading = false;
				});
			},
			
			getCity:function (){
				var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
				});  
				instance.post('admin/common/getAllList').then(data => {
					let  res = data.data;
					if (res.code !== 0) {
						this.$message({
							message: res.msg,
							type: 'error'
						});
					} else {
						this.addrList = res.data;
					}
				});
			},
			//删除
			handleDels(id) {
				this.$confirm('确认删除吗?', '提示', {}).then(() => {
					this.listLoading = true;
					console.log(id, '================')
					var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
					});  
					instance.post('admin/logistics/delNoDeliveryRegion', this.qs.stringify({id: id})).then(data => {
						
						let  res = data.data;
						if (res.code !== 0) {
							this.$message({
								message: res.msg,
								type: 'error'
							});
							this.getData();
						} else {
							this.$message({
								message: '成功',
								type: 'success'
							});
							this.getUsers();
						}
					})
				}).catch(() => {

				});
			},
			//批量删除
			batchRemove() {
				var ids = this.sels.map(item => item.id).toString();
				this.handleDels(ids);
			},
			/** 选择省 */
		    handleProvince(index){
		      this.provinceIndex = index
		      this.cityList = this.addrList[index].children
		      this.addForm.city = ''
		      this.cityIndex = ''
		    },
		    handleCity(index){
		    	this.cityIndex = index
		    },
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					province: '',
					city:'',
					name:'',
					regionId:'',
				};
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							if (this.addForm.city) {
								this.addForm.regionId = this.addForm.city
								this.addForm.name = this.addrList[this.provinceIndex].label + '/' + this.addrList[this.provinceIndex].children[this.cityIndex].label
							}else{
								this.addForm.regionId = this.addForm.province
								this.addForm.name = this.addrList[this.provinceIndex].label
							}
							let para = {
								regionId: this.addForm.regionId,
								name: this.addForm.name,
							};
							var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
							});  
							instance.post('admin/logistics/addNoDeliveryRegion', this.qs.stringify(para)).then(data => {
								
								let  res = data.data;
								if (res.code !== 0) {
									this.$message({
										message: res.msg,
										type: 'error'
									});
								} else {
									this.addLoading = false;
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['addForm'].resetFields();
									this.addFormVisible = false;
									this.getUsers();
								}
							});
						});
					}
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				
				this.editFormVisible = true;
				this.editForm.id = row.id;
				this.editForm.title = row.title;
				this.editForm.content = row.content;
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = {
								id:this.editForm.id,
								title:this.editForm.title,
								content:this.editForm.content,
							}
							var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
							});  
							instance.post('admin/goodsSecurity/upd', this.qs.stringify(para)).then(data => {
								
								let  res = data.data;
								if (res.code !== 0) {
									this.$message({
										message: res.msg,
										type: 'error'
									});
								} else {
									this.editLoading = false;
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getUsers();
								}
							});
						});
					}
				});
			},
		},
		mounted() {
			this.getUsers();
			this.getCity()
		}
	}

</script>

<style scoped>

</style>